import React from 'react'
import { UdTable, UdFilter, IUdColumn, http, IUdFormItem } from '@/lib'
import { Input } from 'antd'
import { WrappedFormUtils } from 'antd/lib/form/Form'
import customerApis from '@/apis/CustomerManage'
export default class SelectPrizePolicyTable extends React.Component<IProps> {
  private filterForm: WrappedFormUtils = {} as WrappedFormUtils
  private conditions: IUdFormItem[] = [
    {
      label: '客户编码',
      id: 'acctCode',
      initialValue: this.props.record.acctCode,
      render: () => {
        return <Input disabled placeholder="请输入客户编码" />
      },
    },
    {
      label: '客户名称',
      id: 'acctName',
      initialValue: this.props.record.acctName,
      render: () => {
        return <Input disabled placeholder="请输入客户名称" />
      },
    },
    // {
    //   label: '政策名称',
    //   id: 'verifyPolicyName',
    // },
  ]
  private columns: IUdColumn<any>[] = [
    ['序号', 'id', (text, record, index) => index + 1, { width: '50px' }],
    { title: '品牌公司编码', dataIndex: 'brandCode', minWidth: '60px' },
    { title: '品牌公司名称', dataIndex: 'brandName', minWidth: '100px' },
    { title: '奖品政策编码', dataIndex: 'verifyPolicyCode', minWidth: '60px' },
    { title: '奖品政策名称', dataIndex: 'verifyPolicyName', minWidth: '100px' },
    { title: '奖品编码', dataIndex: 'prizeCode', minWidth: '60px' },
    { title: '奖品名称', dataIndex: 'prizeName', minWidth: '100px' },
    { title: '终端核销总数', dataIndex: 'writeOffCount', minWidth: '60px' },
    { title: '已兑付数量', dataIndex: 'prizeNumberSum', minWidth: '60px' },
  ]

  state: IState = {
    page: 1,
    size: 10,
    total: 0,
    dataSource: [],
    treeDataList: [],
    loading: false,
  }

  render() {
    let { loading, dataSource,page,size,total } = this.state
    return (
      <>
        <UdFilter
          getForm={form => {
            this.filterForm = form
          }}
          items={this.conditions}
          onSearch={values => {
            this.handleSearch(values)
          }}
          loading={loading}
          resetAfter={this.resetFilterHandle}
        />
        <UdTable
          rowKey="verifyPolicyCode"
          columns={this.columns}
          loading={loading}
          dataSource={dataSource}
          pagination={{
            current: page,
            pageSize: size,
            total: total,
            showSizeChanger: true,
            pageSizeOptions: ['10', '20', '30', '40'],
            showTotal: (total, range) => `共${total}项，当前${range[0]}-${range[1]}项`,
          }}
          onChange={this.handleChangePage}
        ></UdTable>
      </>
    )
  }

  componentDidMount() {
    this.getTableData()
  }

  private onOk = e => {
    e.close()
  }

  private handleSearch = (values: any) => {
    this.getTableData()
  }

  private resetFilterHandle = (form: WrappedFormUtils) => {
    this.setState({ dataSource: [] }, () => {
      this.getTableData()
    })
  }

  private handleChangePage = pagination => {
    const { current: page, pageSize: size } = pagination
    this.setState({ page, size }, () => {
      this.getTableData()
    })
  }

  private getTableData = async () => {
    this.setState({ loading: true })
    let { page, size } = this.state
    let { acctCode, verifyPolicyName } = this.filterForm.getFieldsValue()
    let params = {
      acctCode,
      // verifyPolicyName,
      page,
      size,
    }
    http
      .get(customerApis.getPrizeList, {
        params,
      })
      .then(res => {
        const dataSource = res?.data?.content ?? []
        this.setState({
          dataSource,
          total: res.data.totalElements,
        })
      })
      .finally(() => {
        this.setState({
          loading: false,
        })
      })
  }
}

interface IProps {
  record: Record<string, any>
}
interface IState {
  page: number
  size: number
  total: number
  dataSource: Record<string, any>[]
  loading: boolean
  treeDataList: Record<string, any>[]
  searchValues?: {}
}
